package cn.wolfcode.shop.cloud.redis;

import cn.wolfcode.shop.cloud.common.BusinessException;
import cn.wolfcode.shop.cloud.util.RedisCodeMsg;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

/**
 * redis 里面常用的方法
 */

public class RedisService {
    @Autowired
    JedisPool jedisPool;

    //set
    public <T>void set(KeyPrefix keyPrefix,String key, T data){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (keyPrefix.getExpireTime()>0){
                jedis.setex(realKey, keyPrefix.getExpireTime(), JSON.toJSONString(data));
            }else {
                jedis.set(realKey,JSON.toJSONString(data) );
            }
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //get
    public <T>T get(KeyPrefix keyPrefix,String key, Class<T> clz){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            String redisValue = jedis.get(realKey);
            if (StringUtils.isEmpty(redisValue)){
                return null;
            }
            return JSON.parseObject(redisValue,clz);
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //incr value+1操作
    public <T>long incr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.incr(realKey);
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //decr value-1操作
    public <T>long decr(KeyPrefix keyPrefix,String key){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.decr(realKey);
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //expire设置存活时间
    public <T>long expire(KeyPrefix keyPrefix,String key){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.expire(realKey, keyPrefix.getExpireTime());
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //exists查看状态
    public <T>Boolean exists(KeyPrefix keyPrefix,String key){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.exists(realKey);
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hset
    public <T>void hset(KeyPrefix keyPrefix,String key,String field, T data){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            jedis.hset(realKey,field , JSON.toJSONString(data));
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hget
    public <T>T hget(KeyPrefix keyPrefix,String key,String field, Class<T> clz){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (StringUtils.isEmpty(realKey)){
                return null;
            }
           return JSON.parseObject(jedis.hget(realKey, field),clz);
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }

    //hgetAll
    public <T>Map<String,T> hgetAll(KeyPrefix keyPrefix,String key, Class<T> clz){
        try (Jedis jedis=jedisPool.getResource()){
            String realKey = keyPrefix.getPrefix() + key;
            //如果有过期时间再放到redis的时候要设置过期时间
            if (StringUtils.isEmpty(realKey)){
                return null;
            }
            Map<String, String> stringStringMap = jedis.hgetAll(realKey);

            Map<String,T> data = new HashMap<>();
            for (Map.Entry<String, String> item : stringStringMap.entrySet()) {
                data.put(item.getKey(),JSON.parseObject(item.getValue(),clz) );
            }
            return  data;
        } catch(Exception e) {
            e.printStackTrace();
            throw new BusinessException(RedisCodeMsg.REDIS_ERROR);
        }
    }
}
